var util = require('../../libs/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
      if(e.type){
        this.setData({
          type:e.type
        })
      }
      if(e.vehicle){
        this.setData({
          vehicle:e.vehicle
        })
      }
      if(e.driver){
        this.setData({
          driver:e.driver
        })
      }
      if(this.data.type == 'changeCompany' && this.data.driver){
         this.loadChangeCompanyData()
      }
  },
  loadChangeCompanyData:function(){
    var that = this;
    util.getData("/atapi/vehicle/vehicle/changeCompany", { driver: that.data.driver,vehicle: that.data.vehicle }, function (res) {
      console.log(res.data)
      if (res.data.success) {
         var changeCompany = String(res.data.data[0][0].changeCompany).split("|");
         that.setData({
           driverName:res.data.data[0][0].name,
           plate:res.data.data[1].length > 0?res.data.data[1][0].plate:'无',
           c_CO_ID:changeCompany[0],
           c_CO_NAME:changeCompany[1],
           c_user_id:changeCompany[2],
           c_name:changeCompany[3],
         })
      }
    });
  },
  confirmAddInCompany:function(e){
    var that = this;
    wx.showModal({
      title: '车辆申请加入企业',
      content: '是否确认车辆加入企业？确认后车辆和司机将迁移到企业。',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍候...',
          });
          util.postData("/atapi/vehicle/vehicle/confirmChangeCompany", {
            vehicle: that.data.vehicle,
            plate:that.data.plate,
            driver: that.data.driver,
            driverName:that.data.driverName,
            c_user_id:that.data.c_user_id,
            CO_ID:that.data.c_CO_ID,
            CO_NAME:that.data.c_CO_NAME,
            user_id:app.globalData.user_id,
            truename:app.globalData.truename,
            changeCompany:that.data.changeCompany,
            openid:app.globalData.openid
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading();
            if (error) {
              wx.showToast({
                title: '审核失败',
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
            if (res.data.success) {
              wx.showToast({
                title: "审核通过",
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/index/index'
                    })
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: '审核失败',
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
          });
        } else if (res.cancel) {
        }
      }
    })
  },
  cancelAddInCompany:function(e){
    var  that = this;
    wx.showModal({
      title: '车辆申请加入企业',
      content: '确认不同意本车辆加入企业吗？',
      success: function (res) {
        if (res.confirm) {
          util.postData("/atapi/vehicle/vehicle/cancelChangeCompany", {
            vehicle: that.data.vehicle,
            plate:that.data.plate,
            driver: that.data.driver,
            driverName:that.data.driverName,
            c_user_id:that.data.c_user_id,
            CO_ID:that.data.c_CO_ID,
            CO_NAME:that.data.c_CO_NAME,
            user_id:app.globalData.user_id,
            truename:app.globalData.truename,
            openid:app.globalData.openid
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading();
            if (error) {
              wx.showToast({
                title: '审核失败',
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
            if (res.data.success) {
              wx.showToast({
                title: "审核成功",
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/index/index'
                    })
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: '审核失败',
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
          });
          that.setData({
            modalName:null,
            scrollHeight: that.data.scrollHeight + 205
          });
        }else{
          
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})